<!--
 * @Description: 风险管控审批
 * @Author: wangwangwang
 * @Date: 2020-11-30 17:23:46
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-08-26 14:20:11
-->
<template>
  <div class="flows-content">
    <el-container>
      <el-main style="margin-bottom: 20px; padding: 0;">
        <el-container direction="vertical">
          <FlowHeader :isReadOnly="readOnly" @saveSubmit="handleSubmit(1)" @sendSubmit="handleSubmit(2)" @handleBackEnd="handleSubmit(3)" @handleCC="handleSubmit(4)" @handleAssign="handleSubmit(5)" @handleBackUp="handleSubmit(6)" @handleAssist="handleSubmit(7)" @handleNode="handleSubmit(8)" @handleNodeImage="handleSubmit(9)" @handleCollection="handleSubmit(10)" />
          <el-main class="info">
            <div>
              <el-input v-model="title" class="flow-title" :disabled="$route.query.type*1!==1" placeholder="请输入流程标题"></el-input>
            </div>
            <div class="info-top">
              <div class="left">
                <span>单据号码：{{billCode}}</span>
                <span class="text">制单日期：{{billDate}}</span>
              </div>
              <div class="right">
                <span>发起人：{{applyPerson}}</span>
                <span class="text">状态：{{taskTypeText[taskStatus*1]}}</span>
              </div>
            </div>
            <div style="margin-top:20px;">
              <el-form :disabled="true" :inline="true" :model="formData" label-width="110px">
                <div class="form-title">工程基本信息</div>
                <el-form-item prop="name">
                  <span slot="label">工程名称</span>
                  <el-input v-model="formData.name" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="place">
                  <span slot="label">地理位置</span>
                  <el-input v-model="formData.place" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="expectedAmount" label-width="160px">
                  <span slot="label">预估用砼量（方）</span>
                  <el-input v-model="formData.expectedAmount" style="width: 150px;" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="typeId">
                  <span slot="label">合同单位</span>
                  <el-select v-model="formData.roadworkId" style="width:178px;" placeholder="请选择">
                    <el-option v-for="(item, index) in roadworkIdList" :key="index" :label="item.name" :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="businessTypeId">
                  <span slot="label">业务类型</span>
                  <el-input v-model="formData.businesName" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="executePhase">
                  <span slot="label">执行阶段</span>
                  <el-select v-model="formData.executePhase" style="width: 200px;" placeholder="请选择" disabled>
                    <el-option label="委托交底" :value="1"></el-option>
                    <el-option label="评估" :value="2"></el-option>
                    <el-option label="投标" :value="3"></el-option>
                    <el-option label="合同交底" :value="4"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="typeId">
                  <span slot="label">工程性质</span>
                  <el-input v-model="formData.type" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="rateOfProgressId">
                  <span slot="label">项目进度</span>
                  <el-input v-model="formData.rateOfProgress" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="startTime">
                  <span slot="label">起供时间</span>
                  <el-date-picker v-model="formData.startTime" style="width:200px" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss" disabled></el-date-picker>
                </el-form-item>
                <div>
                  <el-form-item prop="constructionUnit">
                    <span slot="label">建设单位</span>
                    <el-input v-model="formData.constructionUnit" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                  <el-form-item prop="cuLinkman">
                    <span slot="label">联系人</span>
                    <el-input v-model="formData.cuLinkman" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                  <el-form-item prop="cuPhone">
                    <span slot="label">电话</span>
                    <el-input v-model="formData.cuPhone" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                </div>
                <div>
                  <el-form-item prop="roadwork">
                    <span slot="label">施工单位</span>
                    <el-input v-model="formData.contractSigningUnit" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                  <el-form-item prop="roadworklInkman">
                    <span slot="label">联系人</span>
                    <el-input v-model="formData.roadworklInkman" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                  <el-form-item prop="roadworkPhone">
                    <span slot="label">电话</span>
                    <el-input v-model="formData.roadworkPhone" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                </div>
                <div>
                  <el-form-item prop="clientTypeId">
                    <span slot="label">机会类型</span>
                    <el-input v-model="formData.clientType" placeholder="请输入" disabled></el-input>
                  </el-form-item>
                  <el-form-item>
                    <span slot="label">行政区</span>
                    <el-cascader v-model="formData.administrativeDistricts0" style="width:178px;" :options="treeData" filterable></el-cascader>
                  </el-form-item>
                </div>
                <el-form-item prop="tubularColumn">
                  <span slot="label">管柱（米）</span>
                  <el-input v-model="formData.tubularColumn" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <el-form-item prop="fabricated">
                  <span slot="label">装配式</span>
                  <el-input v-model="formData.fabricated" placeholder="请输入" disabled></el-input>
                </el-form-item>
                <div>
                  <el-form-item v-if="UserInfo.adminStatus == 1" prop="createUserId" :rules="[{required: true,message: '请选择负责人',trigger: 'blur',}]" class="FZYdanWei">
                    <span slot="label">负责人</span>
                    <el-cascader ref="getCheckedNodes" v-model="formData.createUserId" style="width:227px;" :options="principalarr" :show-all-levels="false" :props="{ value: 'id', label: 'name' }" :placeholder="createUserName"></el-cascader>
                  </el-form-item>
                </div>
                <el-form-item prop="remark">
                  <span slot="label">备注</span>
                  <el-input v-model="formData.remark" style="width: 920px;" type="textarea" :rows="5" resize="none" placeholder="请输入" disabled></el-input>
                </el-form-item>
              </el-form>
              <el-form label-width="110px" label-position="top">
                <div class="control">
                  <div style="width:35%;">
                    <div class="control-title">限时</div>
                    <div style="padding: 20px">
                      <el-form-item prop="code">
                        <span slot="label">施工工期：</span>
                        <div>起：{{ Riskcontrol.beginTime }}</div>
                        <div>止：{{ Riskcontrol.finishTime }}</div>
                      </el-form-item>
                      <el-form-item v-if="limitset" prop="code">
                        <span slot="label">限时设置：</span>
                        <el-date-picker
                          v-model="Riskcontrol.limitTime"
                          type="daterange"
                          size="small"
                          :disabled='!limitsetEdit'
                          value-format="yyyy-MM-dd"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
                        </el-date-picker>
                      </el-form-item>
                      <el-form-item v-if="(Riskcontrol.limitBeginTime) && (Riskcontrol.limitTime[0]!==Riskcontrol.limitBeginTime || Riskcontrol.limitTime[1]!==Riskcontrol.limitFinishTime)">
                        <div class="delete-line">{{`${Riskcontrol.limitBeginTime}至${Riskcontrol.limitFinishTime}`}}</div>
                      </el-form-item>
                      <el-form-item v-show="limitset" prop="code">
                        <span slot="label">提醒时间：
                          <el-date-picker v-model="Riskcontrol.noticeLimitFinishTime" :disabled='!limitsetEdit' type="datetime" size="small" value-format="yyyy-MM-dd HH:mm:ss" />
                        </span>
                      </el-form-item>
                    </div>
                  </div>
                  <div>
                    <div class="control-title">限量</div>
                    <div style="padding: 20px">
                      <el-form-item prop="code">
                        <span slot="label">预估用砼量：</span>
                        <div>{{ Riskcontrol.estimateNum }}立方米</div>
                      </el-form-item>
                      <el-form-item v-if="limitset" prop="code">
                        <span slot="label">限量设置：（立方米）</span>
                        <el-input v-model="Riskcontrol.limitEstimateNum" :disabled='!limitsetEdit' style="width: 250px" placeholder="请输入" clearable></el-input>
                      </el-form-item>
                      <el-form-item v-if="Riskcontrol.copyRowData.limitEstimateNum&&Riskcontrol.copyRowData.limitEstimateNum!==Riskcontrol.limitEstimateNum">
                        <div class="delete-line">{{Riskcontrol.copyRowData.limitEstimateNum}}</div>
                      </el-form-item>
                      <el-form-item v-if="limitset" prop="code">
                          <span slot="label">提醒量：（立方米）</span>
                          <el-input v-model="Riskcontrol.noticeLimitEstimateNum" :disabled='!limitsetEdit' style="width: 250px" placeholder="请输入"></el-input>
                        </el-form-item>
                    </div>
                  </div>
                  <div>
                    <div class="control-title">限额</div>
                    <div style="padding: 20px">
                      <el-form-item prop="code">
                        <span slot="label">预估成本：{{ Riskcontrol.estimateCost }}万</span>
                      </el-form-item>
                      <el-form-item prop="code">
                        <span slot="label">预估产值：{{ Riskcontrol.forecastEarnings }}万</span>
                      </el-form-item>
                      <el-form-item v-if="limitset" prop="code">
                        <span slot="label">限价设置：（万）</span>
                        <el-input v-model="Riskcontrol.limitPrice" :disabled='!limitsetEdit' style="width: 250px" placeholder="请输入" clearable></el-input>
                      </el-form-item>
                      <el-form-item v-if="Riskcontrol.copyRowData.limitPrice&&Riskcontrol.copyRowData.limitPrice!==Riskcontrol.limitPrice">
                        <div class="delete-line">{{Riskcontrol.copyRowData.limitPrice}}</div>
                      </el-form-item>
                      <el-form-item v-if="limitset" prop="code">
                        <span slot="label">提醒价格：（万）</span>
                        <el-input v-model="Riskcontrol.noticeLimitPrice" :disabled='!limitsetEdit'></el-input>
                      </el-form-item>
                    </div>
                  </div>
                </div>
                <div v-show="false" v-if="limitproduct" style="padding:20px 30px;">
                  <span>是否限制生产：</span>
                  <el-radio v-model="Riskcontrol.isPro" :disabled='!limitproductEdit' :label="0">否</el-radio>
                  <el-radio v-model="Riskcontrol.isPro" :disabled='!limitproductEdit' :label="1">是</el-radio>
                </div>
              </el-form>
            </div>
            <div class="flows-details">
              <FlowContent ref="flowcontent" :title='title' :showSendVisible="showSendVisible" @handleCancel="handleCancel" @setElement="setElement" @sendCode="sendCode" @handleFinish="handleFinish" @handleBusiness="handleBusiness" />
            </div>
          </el-main>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import FlowContent from '@/components/WorkFlow/flowContent';
import FlowHeader from '@/components/WorkFlow/flowHeader';
import { getUserInfo } from "@/utils/auth.js";
import { QueryOneByid, queryAllByCompanyId } from "@/api/internetSales/chanceManagement.js";
import treeData from '@/views/platform/ICPS/basicConfig/address.js';
import { orgUserTree } from "@/api/common.js";
export default {
  components: {
    FlowContent,
    FlowHeader,
  },
  data() {
    return {
      treeData,
      UserInfo: '',
      principalarr: [],
      roadworkIdList: [],
      type: 0,
      title: '风险管控审批',
      billCode: '',
      billDate: '',
      applyPerson: '',
      taskTypeText: ['发起', '已保存', '审批中', '已完成', '撤销', '不通过'],
      taskStatus: 0,
      showSendVisible: false,
      readOnly: false,
      formData: {},
      Riskcontrol: {
        limitTime: [],
        copyRowData: '',
        limitEstimateNum: '',
      },
      limitset: true,
      limitsetEdit: true,
      limitproduct: true,
      limitproductEdit: true,
    }
  },
  async created() {
    this.UserInfo = getUserInfo();
    this.principalarr = await orgUserTree();
    this.treeData.forEach((v) => {
      v.children.forEach((res) => {
        res.children.forEach((res2) => {
          delete res2.children
        })
      })
    })
  },
  methods: {
    handleBusiness() {
      const { query: { dataContent } } = this.$route;
      const data = JSON.parse(dataContent);
      const params = {
        process_success: {
          url: '/icps/system/sign/cloudSell/assess/updateRiskControl',
          data: {
            id: data.keyId,
            limitBeginTime: this.Riskcontrol.limitTime[0],
            limitFinishTime: this.Riskcontrol.limitTime[1],
            limitEstimateNum: this.Riskcontrol.limitEstimateNum,
            limitPrice: this.Riskcontrol.limitPrice,
            noticeLimitFinishTime: this.Riskcontrol.noticeLimitFinishTime,
            noticeLimitEstimateNum: this.Riskcontrol.noticeLimitEstimateNum,
            noticeLimitPrice: this.Riskcontrol.noticeLimitPrice,
            isPro: this.Riskcontrol.isPro,
          },
          httpMethod: 'post',
        },
        process_failure: ''
      }
      this.showSendVisible = false;
      this.$refs.flowcontent.handleToFlow(params);
    },
    /**
     * @Description: 
     * @Param: 1为保存，2为最后提交/3 中间提交
     * @Author: wangwangwang
     * @param {*} type
     */
    handleFinish() {
    },
    handleSubmit(type) {
      const { query } = this.$route;
      if (query.type * 1 === 2 && type === 2 && query.taskType * 1 !== 2) { // 发起流程
        this.showSendVisible = true;
        return
      }
      const data = {
        dataSource: 'RiskControlFlow',
        dataContent: JSON.stringify({
          ...this.Riskcontrol,
        }), // 业务数据
      }
      this.$refs.flowcontent.saveFlows(type, data);
    },
    handleFlowChat() {
      this.$refs.flowcontent.showFlowNode()
    },
    handleCancel() {
      this.showSendVisible = false;
    },
    /**
     * @Description: 回显业务数据
     * @Param: 
     * @Author: wangwangwang
     */
    initFlowsData(data) {
      this.Riskcontrol = { ...data, isPro: data.isPro * 1,copyRowData: JSON.parse(data.copyRowData) };
      QueryOneByid({ id: data.keyId }).then((res) => {
        if (res.code === 1001) {
          this.formData = res.data;
          this.formData.administrativeDistricts0 = JSON.parse(res.data.administrativeDistricts)
        }
      });
    },
    initFlowsData2() {
      const { query: { dataContent }, query } = this.$route;
      const data = JSON.parse(dataContent);
      /**
       * @Description: 处理自动生成流程的数据重构
       * @param {*}
       * @Author: wangwangwang
       */      
      if (!data.limitTime) {
        data.limitTime = [data.limitBeginTime, data.limitFinishTime];
      }
      data.copyRowData = data.copyRowData || {};
      this.Riskcontrol = { ...data, isPro: data.isPro * 1, };
      QueryOneByid({ id: data.keyId }).then((res) => {
        if (res.code === 1001) {
          this.formData = res.data;
          this.formData.administrativeDistricts0 = JSON.parse(res.data.administrativeDistricts)
        }
      });
      this.title = query.taskName;
      this.billCode = query.receiptNumber;
      this.billDate = query.createTime || data.createTime;
      this.applyPerson = query.createUserName || data.createUserName;
      this.taskStatus = query.taskStatus;
    },
    sendCode(code) {
      this.billCode = code;
    },
    /**
     * @Description: 处理审批要素
     * @param {*}
     * @Author: wangwangwang
     */    
    setElement (data) {
      for (let i = 0; i < data.length; i+=1) {
        if (data[i].code === 'limitset' && data[i].hideType !== '1') {
          this.limitset = false;
        }
        if (data[i].code === 'limitset' && !data[i].readonly) {
          this.limitsetEdit = false;
        }
        if (data[i].code === 'limitproduct' && data[i].hideType !== '1') {
          this.limitproduct = false;
        }
        if (data[i].code === 'limitproduct' && !data[i].readonly) {
          this.limitproductEdit = false;
        }
      }
    },
  },
  mounted() {
    const { query } = this.$route;
    queryAllByCompanyId().then((res) => {
      this.roadworkIdList = res.data
    });
    if (query.type * 1 == 1) { // 发起流程
      this.initFlowsData(query);
      this.$refs.flowcontent.initBaseData('RiskControlFlow');
      this.billDate = new Date().Format('yyyy-MM-dd hh:mm:ss');
      this.applyPerson = getUserInfo().userRealName;
      return
    }
    this.initFlowsData2();
    if (query.isReadOnly*1===1) {
      this.readOnly = true;
    }
  }
}
</script>
<style lang="scss" scoped>
.form-title {
  font-size: 18px;
  margin: 10px 0;
}
.control {
  display: flex;
  justify-content: space-around;
  > div {
    width: 300px;
    height: 400px;
    border-radius: 5px;
    border: 1px solid #e1e5ed;
    .control-title {
      font-size: 14px;
      color: #000;
      font-weight: bold;
      background: #e1e5ed;
      @include discen;
      height: 48px;
    }
  }
}
.flows-details {
  margin-top: 20px;
}
.info {
  padding: 0 20px;
  background: #fff;
  .info-top {
    font-size: $font_size;
    color: #000000;
    @include discen_al;
    justify-content: space-between;
    height: 50px;
    border-bottom: 1px #e1e5ed solid;
    .left {
      @include discen_al;
    }
    .text {
      margin-left: 60px;
    }
  }
}
.nav_title {
  @include discen_al;
  align-items: flex-end;
  > div {
    width: 4px;
    height: 24px;
    background: $bg_color;
    margin-right: 10px;
  }
  i {
    margin-left: 5px;
  }
  color: $bg_color;
  font-size: 20px;
  font-weight: bold;
}
</style>

